

using System;
namespace PandaDataLogger
{
  static class CounterCollection
  {
    const string sep = "|";
    const string EOM = "\r\n";
    static string mes = string.Empty;
    static public bool HighRate { get; set; }
    static public bool HighRateTest { get; set; }
    static public bool LastHighRateTest { get; set; }
    static public uint FreeMem { get; set; }

    #region Electricity

    /// <summary>
    /// Electricity Day Counter
    /// </summary>
    static public int EDCntr { get; set; }

    /// <summary>
    /// Return Electricity Day Counter in kwh
    /// </summary>
    
    static public double EDCounter
    {
      get { return intToDouble(EDCntr, 1000); }
    }

    /// <summary>
    /// Electricity Night Counter
    /// </summary>
    static public int ENCntr { get; set; }

    /// <summary>
    /// Return Electricity Night Counter in kwh
    /// </summary>
    static public double ENCounter
    {
      get { 
        return intToDouble(ENCntr, 1000); }
    }

    /// <summary>
    /// Instant Electricity Consumption in watt
    /// </summary>
    static private long edcons;
    static public double EDCons
    {
      get
      {
        if (edcons != 0)
        {
          double cons = 3600F / (((double)(edcons - 92))/1000);
          return  cons;
        }
        else
        { return 0; }
      }
      set { edcons = (long)value; }
    }

    static public void SetERate(DateTime time)
    {
      
      if (time.DayOfWeek == DayOfWeek.Saturday || time.DayOfWeek == DayOfWeek.Sunday)
      {
        HighRate = false;
      }
      else
      {
        if (time.Hour >= 6 & time.Hour < 21)
        {
          HighRate = true;
        }
        else
        {
          HighRate = false;
        }
      }
    }

    #endregion

    #region Gas
    /// <summary>
    /// Gas Counter
    /// </summary>
    static public int GACntr { get; set; }

    /// <summary>
    /// Return Gas counter in m3
    /// </summary>
    static public double GACounter
    {
      get { return intToDouble(GACntr, 100); }
    }

    /// <summary>
    /// Return Instant Gas consumption in m3/h
    /// </summary>
    static private long gacons;
    static public double GACons
    {
      get
      {
        if (gacons != 0 & gacons < 15000)
        { return (.01 / ((double)gacons / 1000) * 3600); }
        else
        { return 0; }
      }
      set { gacons = (long)value; }
    }
    #endregion

    #region Water
    /// <summary>
    /// Water Counter
    /// </summary>
    static public int WACntr { get; set; }

    /// <summary>
    /// Return Water Counter in m3
    /// </summary>
    static public double WACounter
    {
      get { return WACntr * 0.0005; }
    }

    /// <summary>
    /// Return Water Consumption in L/Min
    /// </summary>
    static private long wacons;
    static public double WACons
    {
      get
      {
        if (wacons != 0)
        {
          return 120 / ((double)wacons / 1000);
        }
        else
        { return 0; }
      }
      set { wacons = (long)value; }
    }
    #endregion

    #region Message
    /// <summary>
    /// Return all counter values
    /// </summary>
    /// <returns>string</returns>
    static public string Message()
    {

      mes = HighRate + sep + DateTime.Today.DayOfWeek.ToString()  + sep + EDCons.ToString("F0") + sep + EDCounter.ToString("F2") + sep + ENCounter.ToString("f2") + sep + GACons.ToString("F1") + sep + GACounter.ToString("F2") + sep + WACons.ToString("F1") + sep + WACounter.ToString("F2") + EOM;
      return mes;
    }

    static public string[] cc()
    {
      string[] c = new string[] { HighRate.ToString(), EDCons.ToString("F0"), EDCounter.ToString("F2"), ENCounter.ToString("f2"), GACons.ToString("F1"), GACounter.ToString("F2"), WACons.ToString("F1"), WACounter.ToString("F2") };
      return c;
    }

    #endregion

    private static double intToDouble(int val, int precision)
    {
      double value = (double)val * precision;
      value = System.Math.Round(value);
      value /= (precision * precision);
      return value;
    }

  }
}
